namespace IconPatcher
{
    using System;
    using System.Globalization;
    using System.Linq;

    using Microsoft.Win32;

    public static class VisualStudio2012
    {
        // http://stackoverflow.com/questions/1074411/how-to-open-a-wow64-registry-key-from-a-64-bit-net-application
        private static RegistryKey GetSoftwareRoot()
        {
            var path = IntPtr.Size == 8
                           ? @"Software\Wow6432Node"
                           : @"Software";
            return Registry.LocalMachine.OpenSubKey(path);
        }

        // http://technet.microsoft.com/en-us/library/ee225238(v=vs.110).aspx
        public enum Edition
        {
            Undefined = -1,
            Professional = 1,
            Premium = 2,
            Ultimate = 3
        }

        public static Edition InstalledEdition()
        {
            var rootKey = GetSoftwareRoot().OpenSubKey(@"Microsoft\DevDiv\vs\Servicing\11.0");
            if (rootKey == null)
            {
                return Edition.Undefined;
            }

            var subkeys = rootKey.GetSubKeyNames()
                .OrderByDescending(x => x, StringComparer.OrdinalIgnoreCase);

            var names = Enum.GetNames(typeof(Edition));

            foreach (var name in names)
            {
                if (subkeys.Contains(name, StringComparer.OrdinalIgnoreCase))
                {
                    return (Edition)Enum.Parse(typeof(Edition), name);
                }
            }

            return Edition.Undefined;
        }

        public static bool IsVersionInstalled(Edition edition)
        {
            try
            {
                var key = GetSoftwareRoot().OpenSubKey(@"Microsoft\DevDiv\VS\Servicing\11.0\" + edition);
                return key != null;
            }
            catch
            {
                return false;
            }
        }

        public static string PrimaryLanguage()
        {
            try
            {
                var rootKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\VisualStudio\11.0\General");
                if (rootKey == null)
                {
                    return null;
                }

                var language = (int) rootKey.GetValue("UILanguage", 0);
                if (language == 0)
                {
                    throw new Exception("Unable to determine the primary language.");
                }
                
                return language.ToString(CultureInfo.InvariantCulture);
            }
            catch
            {
                return null;
            }
        }

        public static string[] AllLanguages(Edition edition)
        {
            var rootKey = GetSoftwareRoot().OpenSubKey(@"Microsoft\DevDiv\vs\Servicing\11.0\" + edition);
            if (rootKey == null)
            {
                throw new Exception("Unable to find registry key for the " + edition + " edition.");
            }

            return rootKey.GetSubKeyNames();
        }
    }
}